package com.example.basic.web.service.newlyManage.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.basic.web.mapper.device.DeviceMapper;
import com.example.basic.web.mapper.newly.SysAdmireMapper;
import com.example.basic.web.model.device.Device;
import com.example.basic.web.model.newly.SysAdmire;
import com.example.basic.web.response.SysAdmireResponse;
import com.example.basic.web.service.newlyManage.ISysAdmireManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysAdmireManageServiceImpl implements ISysAdmireManageService {

    @Autowired
    private SysAdmireMapper sysAdmireMapper;

    @Autowired
    private DeviceMapper deviceMapper;





    @Override
    public List<SysAdmireResponse> queryList() {
        LambdaQueryWrapper<SysAdmire> admireLambdaQueryWrapper = new LambdaQueryWrapper<>();
        admireLambdaQueryWrapper.orderByDesc(SysAdmire::getCreateAt);

        List<SysAdmire> admireList = sysAdmireMapper.selectList(admireLambdaQueryWrapper);

        return admireList.stream().map(admire -> {
            SysAdmireResponse response = BeanUtil.toBean(admire, SysAdmireResponse.class);
            Device device = deviceMapper.selectById(admire.getCachePositionId());
            if (device != null) {
                response.setCachePositionName(device.getDeviceName());
            }
            Device admireDevice = deviceMapper.selectById(admire.getId());
            if (admireDevice!= null) {
                response.setAdmirePositionName(admireDevice.getDeviceName());
            }
            return response;
        }).collect(Collectors.toList());

    }
}
